package com.novunda.platform.services;


import com.novunda.platform.common.exception.StatisticsException;
import com.novunda.platform.entities.Audit;
import com.novunda.platform.entities.Statistics;
import com.novunda.platform.entities.StatisticsTime;
import com.novunda.platform.repositories.AuditRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lizy
 */
@Service
@Transactional
public class AuditService extends BaseService {
    /**
     * 审计提示最小值
     */
    private static final int MIN_RECORD_TIME = 1;
    /**
     * 审计提示最大值
     */
    private static final int MAX_RECORD_TIME = 1000000;
    /**
     * 审计最大值错误
     */
    private static final String ERROR_RECORD_NUMBER = "审计最大值必须在不小于0不大于1000000之间!";
    /**
     * 审计告警值错误
     */
    private static final String ERROR_RECORD_ALARM = "审计告警值必须在不小于0不大于审计最大值之间!";

    @Autowired
    private AuditRepository auditRepository;

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 获取默认配置
     *
     * @return 默认值
     */
    public Audit getDefaultAudit() {
        List<Audit> audits = auditRepository.findAll();
        if (audits.size() != 1) {
            auditRepository.delete(audits.get(audits.size() - 1));
        }
        statisticsService.getDefaultStatistics();
        return audits.get(0);
    }

    /**
     * 获取配置
     *
     * @return 配置model
     */
    public Audit getAudit() {
        List<Audit> audits = auditRepository.findAll();
        return audits.get(audits.size() - 1);
    }

    /**
     * 保存采集配置
     *
     * @param audit 配置实体
     * @return 保存的实体
     */
    public Audit saveAudit(final Audit audit) throws StatisticsException {

        // 检查审计最大值是否正确
        if (audit.getRecordNumber() < MIN_RECORD_TIME || audit.getRecordNumber() > MAX_RECORD_TIME) {
            throw new StatisticsException(ERROR_RECORD_NUMBER);
        }
        // 检测审计告警值是否正确
        if (audit.getRecordAlarm() < MIN_RECORD_TIME || audit.getRecordAlarm() > audit.getRecordNumber()) {
            throw new StatisticsException(ERROR_RECORD_ALARM);
        }
        List<Audit> audits = auditRepository.findAll();
        // 保存新的配置
        Audit audit1 = getNewAudit(audit);
        statisticsService.saveStatistics(createStatistics(audit));
        if (audits.size() != 1) {
            auditRepository.delete(audits.get(1));
        }
        return auditRepository.save(audit1);
         

    }

    /**
     * 创建采集审计model
     *
     * @param audit 审计model
     * @return 采集审计model
     */
    private Statistics createStatistics(Audit audit) {
        List<StatisticsTime> newList = new ArrayList<>();
        Statistics statistics = new Statistics();
        statistics.setLivelyTime(36);
        statistics.setRecordStatus(audit.getRecordStatus());
        statistics.setRecordNumber(audit.getRecordNumber());
        statistics.setRecordAlarm(audit.getRecordAlarm());
        statistics.setStatisticsTimes(newList);
        StatisticsTime statisticsTime = new StatisticsTime();
        statisticsTime.setName("defaultTime");
        statisticsTime.setBeginTime("00:00");
        statisticsTime.setEndTime("24:00");
        statisticsTime.setStatistics(statistics);
        newList.add(statisticsTime);
        return statistics;
    }

    /**
     * 创建新的配置信息数据
     *
     * @param audit 配置信息
     * @return 新的配置对象
     */
    private Audit getNewAudit(Audit audit) {
        Audit audit1 = new Audit();
        audit1.setRecordStatus(audit.getRecordStatus());
        audit1.setRecordNumber(audit.getRecordNumber());
        audit1.setRecordAlarm(audit.getRecordAlarm());
        return audit1;
    }
}
